package dnaController.action;

import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;

import javax.mail.Message;
//import javax.mail.PasswordAuthentication;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.struts2.interceptor.RequestAware;

import com.opensymphony.xwork2.ActionSupport;

import dnaDAO.Usuario.UsuarioDAO;
import dnaModel.Usuario;

public class EmailController extends BaseController implements RequestAware {

	private static final long serialVersionUID = 1L;
	
	private Map<String, Object> request;
	
	private long editarUsuarioId;
	
	private String login;
	private String assunto;
	private String conteudo;
	private String emailAdm = "bruna.christina@uniriotec.br";	
	
	//private String from;
	//private String password;

	static Properties properties = new Properties();
	static {
		properties.put("mail.smtp.host", "smtp.gmail.com");
		properties.put("mail.smtp.socketFactory.port", "465");
		properties.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		//properties.put("mail.smtp.auth", "true");
		properties.put("mail.smtp.port", "465");
	}
	
	public String enviarEmail(){		
		
		Usuario usuarioLogado = get_USUARIO_LOGADO();
		if(usuarioLogado == null)
			return "erroLogin";
		
		long id = this.editarUsuarioId;
		Usuario editarUsuario = null;
		try {
			editarUsuario = UsuarioDAO.getInstance().obter(id);
		} catch (SQLException e) {
			e.printStackTrace();
			String mensagem = "Erro ao obter Usuario!";
			request.put("msg", mensagem);
			return ERROR;
		}
		request.put("editarUsuario", editarUsuario);			
	
		return SUCCESS;
	}

	public String salvarEmail() {
		
		Usuario usuarioLogado = get_USUARIO_LOGADO();
		if(usuarioLogado == null)
			return "erroLogin";
		
		String mensagem = null;
		try {
			/*Session session = Session.getDefaultInstance(properties,new javax.mail.Authenticator() {
						protected PasswordAuthentication getPasswordAuthentication() {
							return new PasswordAuthentication(from, password);
						}
					});
			*/
			Session session = Session.getDefaultInstance(properties);
			Message message = new MimeMessage(session);
			//message.setFrom(new InternetAddress(from));
			message.setFrom(new InternetAddress(login));			
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailAdm));
			message.setSubject(assunto);
			message.setText(conteudo);
			Transport.send(message);
		} catch (AddressException e) { 
			mensagem = "Erro ao Enviar Email";
			request.put("msg", mensagem);
			e.printStackTrace();
			return ERROR;
		} catch (MessagingException e) {
			mensagem = "Erro ao conectar na internet.";
			request.put("msg", mensagem);
			e.printStackTrace();
			return ERROR;
		}
		mensagem = "Email enviado com com sucesso!";
		request.put("msg", mensagem);
		return SUCCESS;
	}
	
	public long getEditarUsuarioId() {
		return editarUsuarioId;
	}
	
	public void setEditarUsuarioId(long editarUsuarioId) {
		this.editarUsuarioId = editarUsuarioId;
	}

	public static Properties getProperties() {
		return properties;
	}

	public static void setProperties(Properties properties) {
		EmailController.properties = properties;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getAssunto() {
		return assunto;
	}

	public void setAssunto(String assunto) {
		this.assunto = assunto;
	}

	public String getConteudo() {
		return conteudo;
	}

	public void setConteudo(String conteudo) {
		this.conteudo = conteudo;
	}

	public String getEmailAdm() {
		return emailAdm;
	}

	public void setEmailAdm(String emailAdm) {
		this.emailAdm = emailAdm;
	}

	@Override
	public void setRequest(Map<String, Object> arg0) {
		this.request = arg0;
	}

}
